package com.libing.web;

import com.libing.base.BaseController;
import com.libing.entity.Company;
import com.libing.entity.CompanyVO;
import com.libing.enums.StateEnum;
import com.libing.service.CompanyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 公司信息 Controller
 * @Auther: LEE
 * @Date: Create in 2018/6/29 21:59
 */
@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CompanyService companyService;

    @PostMapping(value = "/", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public StateEnum insert(Company company) {
        logger.debug("新增企业信息 ===>" + company.toString());
        company.setCreateId(getLoginUser().getId());
        company.setCreateName(getLoginUser().getName());
        StateEnum result = companyService.insert(company);
        return result;
    }

    public String update() {
        return "";
    }

    public String delete() {
        return "";
    }

    public String selectAll() {
        return "";
    }

    @GetMapping("/companyList")
    public String companyList(Model model) {
        model.addAttribute("user", getLoginUser());
        return "company-list";
    }

    @PostMapping(value = "/{id}", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Company select(@PathVariable("id") Long id) {
        logger.info("获取企业详细信息，ID=[{}]", id);
        Company company = companyService.selectById(id);
        System.out.println("CompanyController.select" + company);
        return company;
    }

    @GetMapping(value = "/list", produces = {"application/json;charset=UTF-8"})
    public String list(Model model) {
        model.addAttribute("user", getLoginUser());
        List<CompanyVO> list = companyService.selectAllVO();
        System.out.println("CompanyController.list"+list);
        model.addAttribute("list", list);
        return "company-list";
    }

    @PostMapping(value = "/selList", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List<CompanyVO> selList() {
        return companyService.selectAllVO();
    }
}
